package com.irain.pp.tool.redis.spring;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

/**
 * @author WangYanPeng
 * @date 2017/10/30 18:50
 */
public class PpJackson2JsonRedisSerializer extends GenericJackson2JsonRedisSerializer {

    private static ObjectMapper defaultMapper = new ObjectMapper()
            .setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY)
            .enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY)
            .setSerializationInclusion(JsonInclude.Include.NON_NULL)
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    public PpJackson2JsonRedisSerializer() {
        super(defaultMapper);
    }

    @Override
    public Object deserialize(byte[] source) throws SerializationException {
        Object object = super.deserialize(source);
        if (object instanceof Integer) {
            return ((Integer) object).longValue();
        }
        return object;
    }

    public ObjectMapper getDefaultMapper() {
        return defaultMapper;
    }

}
